package cn.cosmosx.pdf;

import cn.cosmosx.base.pdf.Type;
import cn.cosmosx.base.pdf.bean.AlipayFlow;
import cn.cosmosx.base.pdf.bean.WxFlow;
import cn.cosmosx.base.pdf.flow.AlipayFlowUtil;
import cn.cosmosx.base.pdf.flow.FlowWrapper;
import cn.cosmosx.base.pdf.flow.WxFlowUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class PdfRead {
    final static String PATH = "C:\\Users\\geng\\Desktop\\流水证明_20221215_141552.pdf";
    final static String PATH2 = "C:\\Users\\geng\\Desktop\\微信支付交易明细证明(20220919-20221219).pdf";

    public static void main(String[] args) throws IOException {
        printAlipayCashFlow(PATH);
        log.error("===============================================================");
        printWxCashFlow(PATH2);
    }

    static void printAlipayCashFlow(String path) {
        FlowWrapper<AlipayFlow> wrapper = AlipayFlowUtil.read(path);
        log.info("姓名：{}", wrapper.getRealName());
        log.info("账户：{}", wrapper.getAccount());
        log.info("证件号码：{}", wrapper.getIdCardNUmber());
        log.info("起始日期：{}", wrapper.getStartDate());
        log.info("结束日期：{}", wrapper.getEndDate());
        log.info("流水合计（收入）：{}", sumAlipayFlow(wrapper, Type.IN));
        log.info("流水合计（支出）：{}", sumAlipayFlow(wrapper, Type.OUT));
        log.info("流水合计（其他）：{}", sumAlipayFlow(wrapper, Type.OTHER));
    }

    static void printWxCashFlow(String path) {
        FlowWrapper<WxFlow> wrapper = WxFlowUtil.read(path);
        log.info("姓名：{}", wrapper.getRealName());
        log.info("账户：{}", wrapper.getAccount());
        log.info("证件号码：{}", wrapper.getIdCardNUmber());
        log.info("起始日期：{}", wrapper.getStartDate());
        log.info("结束日期：{}", wrapper.getEndDate());
        log.info("流水合计（收入）：{}", sumWxFlow(wrapper, Type.IN));
        log.info("流水合计（支出）：{}", sumWxFlow(wrapper, Type.OUT));
        log.info("流水合计（其他）：{}", sumWxFlow(wrapper, Type.OTHER));
    }

    // 计算流水
    static double sumAlipayFlow(FlowWrapper<AlipayFlow> wrapper, Type type) {
        return wrapper.getDetails().stream().filter(f -> type.getValue().equals(f.getOutInType()))
                .mapToDouble(f -> Double.parseDouble(f.getAmount())).sum();
    }
    static double sumWxFlow(FlowWrapper<WxFlow> wrapper, Type type) {
        return wrapper.getDetails().stream().filter(f -> type.getValue().equals(f.getOutInType()))
                .mapToDouble(f -> Double.parseDouble(f.getAmount())).sum();
    }

}
